package com.cheng.reggie.controller;

import com.cheng.reggie.common.R;
import com.cheng.reggie.dto.SetmealDto;
import com.cheng.reggie.entity.Page;
import com.cheng.reggie.entity.Setmeal;
import com.cheng.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired(required = false)
    private SetmealService setmealService;

    @RequestMapping("/list")
    public R<List<Setmeal>> findBySetmealId(Long categoryId, Integer status){
        List<Setmeal> setmealList = setmealService.findBySetmealId(categoryId, status);
        return R.success(setmealList);
    }

    @DeleteMapping
    public R<String> deleteByIds(@RequestParam List<Long> ids,HttpSession session){
        //获取操作人
        Long id = (Long) session.getAttribute("employee");
        //调用业务层方法去删除
        setmealService.deleteByIds(ids,id);
        return R.success("删除套餐成功");
    }


    @GetMapping("/page")
    public R<Page<SetmealDto>>  findPage(Integer page, Integer pageSize, String name){
        //调用业务层查询套餐分页数据方法
        Page<SetmealDto> pageObj = setmealService.findPage(page, pageSize, name);
        //返回封装数据
        return R.success(pageObj);

    }

    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto, HttpSession session){
        //1.获取登录id
        Long id = (Long) session.getAttribute("employee");
        //完善数据(修改人,创建人)
        setmealDto.setCreateUser(id);
        setmealDto.setUpdateUser(id);
        //3.调用业务实现添加套餐
        setmealService.save(setmealDto);
        //封装数据返回

        return R.success("添加套餐成功");
    }
    //根据套餐id查询套餐数据
    @GetMapping("/{id}")
    public R<Setmeal> findById(@PathVariable Long id){
        Setmeal byId = setmealService.findById(id);
        return  R.success(byId);
    }

    @PutMapping
    public R<String> updateById(@RequestBody SetmealDto setmealDto,HttpSession session){
        Long UserId = (Long) session.getAttribute("employee");
        //完善数据
        setmealDto.setUpdateUser(UserId);
        setmealService.updateById(setmealDto);
        return R.success("修改套餐成功!");
    }
}
